/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package org.aroundme.unittests;

import java.util.List;

import org.aroundme.model.Category;
import org.aroundme.persistency.CategoryManager;
import org.aroundme.unitTools.TestFixture;
import org.aroundme.unitTools.UniqueIdGenerator;
import org.aroundme.utility.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Session;

public class CategoryManagerTest extends TestFixture {
	private CategoryManager manager = null;

	protected void setUp() throws Exception {
		super.setUp();
		manager = new CategoryManager();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
		manager = null;
	}
	
	
	public void testGetCategoryByName(){
		Category cat = new Category();
		cat.setName("my test category " + new UniqueIdGenerator().getUniqueId());
		
		//persist it down
		Session s = HibernateUtil.getSessionFactory().getCurrentSession();
		s.beginTransaction();
		try{
			s.saveOrUpdate(cat);
			s.beginTransaction().commit();
		}catch(HibernateException e){
			s.getTransaction().rollback();
			throw e;
		}
		
		//should have a valid id now
		assertTrue("category id should be > 0", cat.getId() > 0);
		
		Category retrieved = manager.getCategoryByName(cat.getName());
		
		assertNotNull("retrieved category shouldn't be null", retrieved);
		assertEquals("id of the persisted and retrieved should be equal", cat.getId(), retrieved.getId());
		assertEquals("name of the persisted and retrieved should be equal", cat.getName(), retrieved.getName());
	}
	
	public void testRetrieveAllCategories(){
		List<Category> retrievedCategories = manager.retrieveAllCategories();
		
		assertNotNull("retrieved list shouln't be null", retrievedCategories);
		assertTrue("retrieved list should contain elements", retrievedCategories.size() > 0);
	}

}
